//function LastUsedLocationCalculator(repository) {
//    this._repository = repository;
//}
//
//LastUsedLocationCalculator.prototype = {
//    getLastUsedLocationsFor: function(currentDate) {
//        var startYearMonth = YearMonth.fromDate(currentDate);
//
//        var me = this;
//
//        var locationHashSet = {};
//        var lastUsedLocations = [];
//
//        for (var i = 0; i < 12; i++) {
//            var yearMonth = startYearMonth.addMonths(-i);
//            var monthModel = this._repository.getMonthByYearMonth(yearMonth);
//
//            if (lastUsedLocations.length >= pwcconfig.LAST_USED_LOCATION_NUMBER) {
//                break;
//            }
//
//            monthModel.eachDayDesc(function(day) {
//                if (lastUsedLocations.length >= pwcconfig.LAST_USED_LOCATION_NUMBER) {
//                    return;
//                }
//
//                if (day.getDate() > currentDate) {
//                    return;
//                }
//
//                var dayLocations = [day.getLocation()].concat(day.getLocationsOnTravels());
//                dayLocations.foreach(function(location) {
//                    if (location) {
//                        locationHashSet[location.id] = location;
//                        lastUsedLocations = me._getValuesForHashSet(locationHashSet);
//                    }
//
//                    if (lastUsedLocations.length >= pwcconfig.LAST_USED_LOCATION_NUMBER) {
//                        return;
//                    }
//                })
//            });
//        }
//
//        return lastUsedLocations.slice(0, pwcconfig.LAST_USED_LOCATION_NUMBER);
//    },
//
//    _getValuesForHashSet : function(hashSet) {
//        var values = [];
//        for (var key in hashSet) {
//            values.push(hashSet[key]);
//        }
//
//        return values;
//    }
//};

function LastUsedLocationCalculator(repository) {
    this._repository = repository;
}

LastUsedLocationCalculator.prototype = {
    getLastUsedLocationsFor: function(currentDate) {
        var startYearMonth = YearMonth.fromDate(currentDate);

        var lastUsedLocations = [];

        var previousAddedLocation = null;
        for (var i = 0; i < 12; i++) {
            var yearMonth = startYearMonth.addMonths(-i);
            var monthModel = this._repository.getMonthByYearMonth(yearMonth);

            monthModel.eachDayDesc(function(day) {
                if (day.getDate() > currentDate) {
                    return;
                }

                var dayLocations = [day.getLocation()].concat(day.getLocationsOnTravels());
                dayLocations.foreach(function(location) {
                    if (location && location != previousAddedLocation &&
                            lastUsedLocations.length < pwcconfig.LAST_USED_LOCATION_NUMBER &&
                            !lastUsedLocations.contains(location)) {
                        lastUsedLocations.push(location);
                        previousAddedLocation = location;
                    }
                 })
             });

             if (lastUsedLocations.length >= pwcconfig.LAST_USED_LOCATION_NUMBER) {
                 break;
             }
        }

        return lastUsedLocations.slice(0, pwcconfig.LAST_USED_LOCATION_NUMBER);
    }
};